<?php

namespace Common\Services;

use Common\BaseService;
use Common\Exception\BaseException;
use Common\Helpers\DebugHelper;
use Common\Libs\Jwt\Exception;
use Common\Services\Sys\ProjectService;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Hyperf\Guzzle\CoroutineHandler;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AuthServer extends BaseService
{

    public  function checkProjectAuth($project_sn,$domain,$token): array
    {
        try {
            /**
             * 校验域名
             */
            $project = ProjectService::getProjectByDomain($project_sn,$domain);
            if (!$project){
                return $this->responseData(401,[
                    'domain'=>$domain
                ],'域名未授权或请求地址地址中project_sn错误，请联系技术');
            }

            /**
             * 校验token
             */
            $client = new Client([
                'base_uri' => 'https://www.dandicloud.cn/',
                'handler' => HandlerStack::create(new CoroutineHandler()),
                'timeout' => 5,
                'swoole' => [
                    'timeout' => 10,
                    'socket_buffer_size' => 1024 * 1024 * 2,
                ],
            ]);
            $promise = $client->postAsync('/admin/user/userinfo', [
                'form_params' => [],
                'headers' => [
                    'access-token'=>$token,
                    'bloc-id'=>1,
                    'store-id'=>1
                ]
            ]);
            $response = $promise->wait();
            $ResData = $response->getBody()->getContents();
            $Res = json_decode($ResData,true);
            if ((int) $Res['code'] === 200){
                return $this->responseData(200,[
                    'project'=>$project,
                    'user'=>$Res['data']
                ],'项目权限校验成功');
            }else{
                return $this->responseData(401,$Res['data']??[],$Res['message']);
            }
        } catch (Exception $e) {
            throw new BaseException(401,$e->getMessage());
        } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
            throw new BaseException(401,$e->getMessage());
        }

    }

    /**
     * 根据域名与app_id 校验用户权限
     */
    public function checkProjectAppID($project_sn,$domain,$appId): array
    {
        try {
            /**
             * 校验域名
             */
            $project = ProjectService::getProjectByAppId($project_sn,$appId);

            if (!$project){
                return $this->responseData(401,[
                    'domain'=>$domain
                ],'域名未授权或请求地址地址中project_sn错误，请联系技术');
            }
            return $this->responseData(200,[
                'project'=>$project
            ],'项目权限校验成功');

        } catch (\Exception $e) {
            throw new BaseException(401,$e->getMessage());
        } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
            throw new BaseException(401,$e->getMessage());
        }
    }
}